<template>
  <div>
    <el-dialog title="新增用户"
               @close="closeAdd"
               :visible.sync="dialogTableVisible"
               width="80%"
               :append-to-body="true">
      <div class="add-user">
        <el-form :model="requestParams"
                 ref="requestParams"
                 label-width="110px">
          <el-row>
            <el-col :span="6"
                    class="left-tree">
              <!-- 人员库组织架构树 -->
              <OrganizationTree :assignedNode.sync="assignNode"
                                :isSupportOpera.sync="isSupportOpera"
                                :assignedTemplateNode.sync="assignTemplateNode"
                                :assignedPermissionNode.sync="assignPermissionNode"
                                :curMenu.sync="selectedNode"></OrganizationTree>
              <!-- <LibraryPeOrgTree :assignedNode.sync="assignNode"
                              :assignedTemplateNode.sync="assignTemplateNode"
                              :assignedPermissionNode.sync="assignPermissionNode"
                              :curMenu.sync="selectedNode"></LibraryPeOrgTree> -->
            </el-col>

            <el-col :span="18">
              <p class="person-list-margin">人员列表</p>
              <JobUserList ref="jobUserList"
                           :isDataAuthPage.sync="isDataAuthPage"
                           :showEditRow="false"
                           :selectedArr.sync="selectedArr"
                           :checkbox="true"
                           :requestParam="getJobUserListParam"></JobUserList>
            </el-col>
            <el-col :span="24"
                    class="bottom-box">
              <el-button :type="isSure()?'info':'primary'"
                         :disabled="isSure()?true:false"
                         @click="saveAddEvaluate">确 定</el-button>
              <el-button @click="cancelEvaluate">取 消</el-button>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { organizationApi } from '@/services/CompanySetting'
import JobUserList from '../../../ConfigCenter/CompanySetting/components/JobUserList';
import OrganizationTree from '@/components/OrganizationTree/OrganizationTree'
import LibraryPeOrgTree from './LibraryPeOrgTree'
import { backEvaluateApi } from '@/services/ProjectCenter'
export default {
  components: {
    LibraryPeOrgTree,
    OrganizationTree,//公共中心的树
    JobUserList,
  },
  props: {
    callBack: Function,
    childDialog: {
      type: Boolean,
      default: () => {
        return false
      }
    }
  },
  data () {
    return {
      treeHeight: '600px',
      isSupportOpera: false,//操作框不展示
      selectedArr: [],
      isDataAuthPage: true,
      // 获取人员列表请求参数
      getJobUserListParam: {
        company_id: '',
        org_no: '',
        dept_no: '',
        job_id: '',
        node_type: '',
        user_name: '',
        is_page: 1,
        page: 1,
        page_size: 10
      },
      requestParams: {
        eva_person: false
      },
      // 当前选择的组织树节点数据
      selectedNode: {},
      // 右键分配权限时选中的节点
      assignPermissionNode: {},
      // 右键分配模板时选中的节点
      assignTemplateNode: {},
      // 右键分配人员时选中的节点
      assignNode: {},
      addRequestParam: {
        page: 1,
        page_size: 10,
      },
      addEvaluateList: {
        list: [{}, {}, {}],
        total_count: 0
      },
      //选中的list
      getRowKeys (row) {
        //记录每行的key值
        return row.person_id;
      },
      dialogTableVisible: false,
      startrequestParam: {
        comp_id: '',
      }
    }
  },
  methods: {
    isSure () {//是否可以点击确定
      let isSure = true;
      if (this.selectedArr.length > 0) {
        isSure = false;
      };
      return isSure;
    },
    // 获取组织树数据
    getOrgTree () {
      organizationApi.getOrgTree({

      }).then(res => {
        let data = res.data
        this.$store.dispatch('setOrgainzationTree', data)
        window.localStorage.setItem('platformOrganizationTree', JSON.stringify(data))
      }, err => {
        console.log(err)
      })
    },
    getIsDisabled () {
      let judge = this.checkIds.length > 0 ? false : true;
      return judge;
    },
    changeCheck (selection, row) {
      let selected = selection.length && selection.indexOf(row) !== -1;
      //selected是当前点击的选中态
      //选中的表格取消事件
      if (selected === false) {
        this.$nextTick(() => {
          this.$refs.multipleTable.selection.forEach((ele) => {
            if (ele.person_id == row.person_id) {
              this.$refs.multipleTable.toggleRowSelection(ele, false);
            }
          });
        });
      }
    },
    cancelEvaluate () {//取消
      this.initResetCommonFunc();//初始化通知函数
    },
    initResetCommonFunc () {//初始化通知函数
      this.addRequestParam = this.$options.data().addRequestParam;//重置     
      this.addOrgan = this.$options.data().addOrgan;//重置
      this.$emit('update:childDialog', false);
    },
    saveAddEvaluate () {//确定
      let ids = this.checkIds.join(',')
      backEvaluateApi.addUser({
        data: {
          user_ids: ids
        }
      }).then((res) => {
        if (res.code === 0) {
          this.callBack();
          this.$message.success('添加成功');
          this.initResetCommonFunc();//重置
        }
      }).catch(err => {
        console.log(err);
      })
    },
    //关闭弹窗
    closeAdd () {
      this.checkIds = [];
      this.initResetCommonFunc();//初始化通知函数
    },
    getCheckIds (arr) {
      let ids = [];
      if (arr.length > 0) {
        arr.map(item => {
          ids.push(item.user_id)
        })
      };
      return ids;
    }
  },
  watch: {
    selectedArr (n, o) {
      this.checkIds = [];
      if (n.length > 0) {
        this.checkIds = this.getCheckIds(n);
      }
    },
    // 组织树选择节点时，根据节点类型获取权限清单及人员列表
    selectedNode: {
      handler (newval, oldval) {
        this.qrType = newval.node_type //是单位还是部门,1是单位,2是部门
        let tempData = {};
        tempData.node_type = newval.node_type
        tempData.user_name = ''
        tempData.company_id = newval.company_id;
        if (newval.node_type === '1') {
          tempData.dept_no = '';
          tempData.job_id = '';
          //详情参数
          tempData.org_no = newval.org_no
        } else if (newval.node_type === '2') {
          tempData.job_id = newval.id  //部门,岗位都要获取id
          //详情参数
          tempData.dept_no = newval.no
          tempData.org_no = newval.parent_no
        } else if (newval.node_type === '3') {
          tempData.job_id = newval.id
          tempData.dept_no = newval.parent_no
        }
        this.getJobUserListParam = Object.assign(this.getJobUserListParam, tempData)
      },
      deep: true
    },
    childDialog (n, o) {
      this.$refs.multipleTable.clearSelection();
      this.dialogTableVisible = n;
    }
  },
  created () {
    this.getOrgTree();
    this.dialogTableVisible = this.childDialog;
  }
}
</script>

<style lang="scss" scoped>
.add-user {
  .person-list-margin {
    margin-bottom: 20px;
  }
  .left-tree {
    padding-right: 20px;
  }
  .bottom-box {
    margin-top: 20px;
    text-align: center;
  }
}
</style>